<?php
declare (strict_types=1);

namespace app\command;

use app\webapi\controller\APiRedisServer;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class rushLogTask extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('rushlogtask')
            ->setDescription('抢购日志');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('抢购日志');
        $data = [];
        $allSchedules = Db::query("SELECT * FROM sy_rush_schedule ORDER BY starttime ASC", []);
        if (count($allSchedules) > 0) {
            foreach ($allSchedules as $v) {
                $count = APiRedisServer::selected(3)->hlen("LOG_S_".$v['id']);
                $success_count = APiRedisServer::selected(3)->hlen("LOG_S_OK_".$v['id']);
                $data[] = array(
                    'sid'=> $v['id'],
                    'starttime'=> $v['starttime'],
                    'endtime'=> $v['endtime'],
                    'name'=> $v['name'],
                    'count'=> $count,
                    'success_count'=> $success_count
                );
                APiRedisServer::selected(3)->del("LOG_S_".$v['id']);
                APiRedisServer::selected(3)->del("LOG_S_OK_".$v['id']);
            }

        }
        Db::execute("INSERT INTO sy_z_log SET type=1,log=?", [json_encode($data, JSON_UNESCAPED_UNICODE)]);
        $output->writeln('抢购日志落盘');
    }
}
